<?
/*
 * This collection of functions (libary) is licensed under GPL2.
 * See LICENSE or www.gnu.org for more details.
 *
 * Author: Henri Ranki
*/


include_once("sodebug.php");
include_once("dzgeneric.php");
include_once("dzview.php");


/** A class to store views in the dzSession class
 *  Every form in the dzSession has view stack. View stack stores views in data
 *  structure that functions lik a a stack. The topmost view is the active
 *  view.
 *
 *  New sub views can be opened on the top of the current view. The dzViewStack
 *  class takes care of controlling of the views on the form. This class also
 *  has methods for creating new and deleting old views.
 */
class dzViewStack {
    var $Id;            /* View id used to identify views in dzSession */

    /* private properties */
    var $__ViewStack;     /** soList A stack that contains views and its subviews */
    var $__SessionInfo;     /* &dzSessionInfo Owner session's basic information */


    /**
     * >Id:int      Id for this view. Id is the same for the subviews
     * >BasicSessionInfo:dzSessionInfo The number of the owner session
     */
    function dzViewStack($Id, $BasicSessionInfo) {
        soDebug("dzViewStack: Entered constructor", SOD_DET);
        $this->Id = $Id;
        $this->__ViewStack = new soList();
        $this->__SessionInfo = $BasicSessionInfo;
    } // dzViewStack

    
    /** Store the given view as the topmost in the view stack
     * >View:dzView View to store
     */
    function setTopView($View) {
        $top_ind = $this->__ViewStack->count();
        /* If no view given or the stack is empty, exit */
        if (($View == NULL) && ($top_ind == 0))return;  // <-- EXIT!!
        $this->__ViewStack->setItem($View, $top_ind-1);
    } // setTopView
    
    
    /** get the topmost view in the view stack
     * >:dzView topmost view or NULL if the stack is empty
     */
    function getTopView() {
        /* Check that stack has atleast one view left and return the
           topmost view. If there is no views return NULL. */
        $top = $this->__ViewStack->count();
        if ($top > 0 )
            $result = $this->__ViewStack->getItem($top-1);
        else
            $result = NULL;

        return $result;
    } //getTopView


    /** Pushes a new view into the view stack. The view at the bottom of the
     *  stack is the base view. Views on the top of that are its subviews.
     *
     *  @param dzView $HotView Current view that is stored in the stack
     *  @param dzviewData $ViewData Basic data for the view
     *  @return dzView: The new view
     */
    function pushView($HotView, $ViewData) {
        /* Store the current view if any given */
        if ($HotView != NULL)
            $this->setTopView($HotView);

        $new_view = new dzView($this->__SessionInfo,
                               $ViewData->SrcFile,
                               $ViewData->Type);

        $new_view->Template->setViewData(DZ_VI_DISPOSABLE, $ViewData->Disposable);
        $new_view->Template->setViewData(DZ_VI_SYS_VIEW, $ViewData->System);

        switch ($ViewData->Type) {
            case DZ_VIEW_TYPE_PSEUDO:
                soDebug("dzViewStack->insertView: Create a pseudo view", SOD_DET);
                break;

            case DZ_VIEW_TYPE_PICK_UP:
            case DZ_VIEW_TYPE_DATAXI:
                soDebug("dzViewStack->insertView: Create dataxi view", SOD_DET);
                break;

            case DZ_VIEW_TYPE_DIALOG:
                soDebug("dzViewStack: Added dialog view. Source=".$ViewData->SrcFile, SOD_DET);
                break;
        }

        $this->__ViewStack->insItem($new_view);

        return $new_view;
    } // pushView
    

    /**
     * Dispose the current view and return the next view in the stack
     * (next view in the view stack).
     * <:dzView     Topmost view or NULL if the stack is empty.
     */
    function popView() {
        $top = $this->__ViewStack->count();
        if ($top > 1) $this->__ViewStack->delItem($top-1);
        return $this->getTopView();
    } // popView


    /**
     * Dispose all the disposable views from the top of the view stack.
     * <:dzView     Topmost view or NULL if the stack is empty.
     */
    function disposeViews() {
        $result = 0;
        $top = $this->__ViewStack->count() - 1;
        do {
            $view = $this->__ViewStack->getItem($top);
            $disposable = $view->Template->getViewData(DZ_VI_DISPOSABLE);
            if ($disposable) {
                $this->__ViewStack->delItem($top);
            }
            $top--;
        } while ($disposable && $top > 0 );

        return $this->getTopView();
    } // disposeViews


    /** Number of views in the stack
     * <:int View count
     */
    function getViewCount() {
        return $this->__ViewStack->count();
    } // getViewCount
    
}; // dzViewStack

?>
